package com.szgr.baoyin.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.szgr.baoyin.R;
import com.szgr.baoyin.activity.StoreDeatilActivity;
import com.szgr.baoyin.model.DetailConfig;

import java.util.List;

/**
 * Created by yang on 15/11/10.
 */
public class StoreDetailListAdapter extends BaseAdapter {
    private Context mContext;
    private List<DetailConfig> configs;
    private View.OnClickListener listener;
    public StoreDetailListAdapter(Context context, List<DetailConfig> configs,View.OnClickListener listener){
        mContext = context;
        this.configs = configs;
        this.listener = listener;
    }
    @Override
    public int getCount() {
        return configs.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if(convertView == null){
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_store_detail_list_view,parent,false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }
        holder = (ViewHolder) convertView.getTag();

        DetailConfig detailConfig = configs.get(position);
        DetailConfig.DetailConfigItem checkedItem = detailConfig.getCheckedItem();
        holder.tv_name.setText(detailConfig.getName());
        if(checkedItem != null) {
            holder.tv_config.setText(checkedItem.getName());
            holder.tv_price.setText("¥ " + checkedItem.getPrice() + "元");
        }
        holder.tv_config.setTag(position);
        holder.tv_config.setOnClickListener(listener);
        holder.tv_config_01.setTag(position);
        holder.tv_config_01.setOnClickListener(listener);

        return convertView;
    }

    public void setConfigs(List<DetailConfig> configs) {
        this.configs = configs;
        notifyDataSetChanged();
    }

    public class ViewHolder{
        TextView tv_name;
        TextView tv_config;
        ImageView tv_config_01;
        TextView tv_price;

        public ViewHolder(View view){
            tv_name = (TextView) view.findViewById(R.id.config_name);
            tv_config = (TextView) view.findViewById(R.id.config_choose);
            tv_price = (TextView) view.findViewById(R.id.config_price);
            tv_config_01 = (ImageView) view.findViewById(R.id.config_choose_01);
        }
    }
}
